package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public List<SetmealDish> findWithDish(Long id) {
        //查询出该套餐下有哪些菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(queryWrapper);
        for (SetmealDish setmealDish : setmealDishes) {
            LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Dish::getId,setmealDish.getDishId());
            Dish dish = dishMapper.selectOne(queryWrapper1);
            setmealDish.setImage(dish.getImage());
        }
        return setmealDishes;
    }

    @Override
    public List<Setmeal> findWithType(Long categoryId, Integer status) {
        //根据两个参数查询setmeal,然后再查出菜品规格
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,categoryId)
                    .eq(Setmeal::getStatus,status);
        List<Setmeal> setmeals = setmealMapper.selectList(queryWrapper);
        for (Setmeal setmeal : setmeals) {
            LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> setmealDishes = setmealDishMapper.selectList(queryWrapper1);
            setmeal.setSetmealDishes(setmealDishes);
        }

        return setmeals;
    }

    @Override
    public void update(Setmeal setmeal) {
        //修改setmeal表中的内容
        int i = setmealMapper.updateById(setmeal);
        //修改套餐下的菜品内容，删了再加
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(queryWrapper);
        for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public Setmeal findById(Long id) {
        //这里需要查三个表
        //套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //根据分类id查询分类名称
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getId,setmeal.getCategoryId());
        Category category = categoryMapper.selectOne(queryWrapper);
        setmeal.setCategoryName(category.getName());
        //根据套餐id查询套餐菜品3
        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(queryWrapper1);
        setmeal.setSetmealDishes(setmealDishes);
        return setmeal;
    }

    @Override
    public Boolean delete(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        //删除两个表中的内容，一个是删除套餐下的菜品，一个是删除套餐本身
        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(queryWrapper1);
        setmealMapper.delete(queryWrapper);
        return true;
    }

    @Override
    public void statusConvert(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmealMapper.update(setmeal,queryWrapper);
    }

    @Override
    public Boolean add(Setmeal setmeal) {
        //先添加套餐，然后再添加套餐下的菜品
        setmealMapper.insert(setmeal);
        if (CollectionUtil.isNotEmpty(setmeal.getSetmealDishes())){
            for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
        return true;
    }

    @Override
    public Page<Setmeal> findByPageAndName(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        Page<Setmeal> page = new Page<>(pageNum,pageSize);
        queryWrapper.like(StringUtils.isNoneEmpty(name),Setmeal::getName,name);
        List<Setmeal> SetmealList = setmealMapper.selectList(queryWrapper);
        Page<Setmeal> pageResult = setmealMapper.selectPage(page, queryWrapper);
        return pageResult;
    }
}
